# -*- coding: utf-8 -*-
"""
@Time ： 2023/10/4 0004 9:03
@Auth ： ysc
@File ：image_build_page.py
@IDE ：PyCharm
"""
import time
from Page.base_page import BasePage
from Page.login_page import LoginPage
from Lib.readelement import Element
from Lib.log import log

image_build = Element('image_build')


class ImageBuildPage(BasePage):
    """镜像定制页面"""
    group_name = None
    PipelineName = None
    pipeline_type_name = None
    format = None

    def login(self, url=None, name=None, password=None):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.login(url, name, password)

    def pipeline_button_info(self):
        """
        页面信息检查--添加流水线按钮
        """
        try:
            pipeline_button = self.find_element(image_build["pipeline_button"])
            if pipeline_button.is_displayed():
                button_info = '添加流水线按钮显示正常'
            else:
                button_info = '用户未登录，添加流水线按钮不显示'
            return button_info
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def pipeline_yaml_info(self):
        """
        页面信息检查--上传yaml文件按钮
        """
        try:
            pipeline_yaml = self.find_element(image_build["pipeline_yaml"])
            if pipeline_yaml.is_displayed():
                yaml_info = '上传yaml按钮显示正常'
            else:
                yaml_info = '用户未登录，上传yaml按钮按钮不显示'
            return yaml_info
        except Exception as e:
            log.error("error: found pipeline_yaml timeout")
            log.info(e)

    def pipeline_filter_info(self):
        """
        页面信息检查—-显示输入框
        """
        try:
            filter_box = self.find_element(image_build["filter_box"])
            if filter_box.is_displayed():
                filter_info = '显示输入框'
            else:
                filter_info = '输入框不显示'
            return filter_info
        except Exception as e:
            log.error("error: found filter_box timeout")
            log.info(e)

    def pipeline_query(self, box_name):
        try:
            self.send_keys(image_build["filter_box"], box_name)
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def select_data(self):
        try:
            self.enter_key(image_build["filter_box"])
        except Exception as e:
            log.error("error: found select button timeout")
            log.info(e)

    def pipeline_select_info(self):
        """
        页面信息检查—--显示下拉框
        """
        try:
            select_box = self.find_element(image_build["select_box"])
            if select_box.is_displayed():
                select_info = '显示下拉框'
            else:
                select_info = '下拉框不显示'
            return select_info
        except Exception as e:
            log.error("error: found filter_box timeout")
            log.info(e)

    def pipeline_select_button(self):
        """首页流水线下拉按钮"""
        try:
            self.click(image_build["select_box"])
            self.mouse_move(image_build["select_box"])
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def pipeline_query_type(self, query_type=None):
        """流水线查询类型"""
        try:
            if query_type == 'pipeline_name':
                self.click(image_build['select_name'])
            elif query_type == 'pipeline_group':
                self.click(image_build['select_group'])
            elif query_type == 'creator':
                self.click(image_build['select_creator'])
            else:
                log.info("query_type is not exist")
        except Exception as e:
            log.error("error: found pipeline_type timeout")
            log.info(e)

    def all_computing_scene(self):
        """
        所有场景
        """
        try:
            self.click(image_build["pipeline_all"])
        except Exception as e:
            log.error("error: found pipeline_all timeout")
            log.info(e)

    def server_scene(self):
        """
        服务器场景
        """
        try:
            self.click(image_build["pipeline_server"])
        except Exception as e:
            log.error("error: found pipeline_server timeout")
            log.info(e)

    def edge_scene(self):
        """
        边缘计算场景
        """
        try:
            self.click(image_build["pipeline_edge"])
        except Exception as e:
            log.error("error: found pipeline_edge timeout")
            log.info(e)

    def cloud_scene(self):
        """
        云计算场景
        """
        try:
            self.click(image_build["pipeline_cloud"])
        except Exception as e:
            log.error("error: found pipeline_cloud timeout")
            log.info(e)

    def embedded_scene(self):
        """
        嵌入式场景
        """
        try:
            self.click(image_build["pipeline_embedded"])
        except Exception as e:
            log.error("error: found embedded_scene timeout")
            log.info(e)

    def get_count(self):
        try:
            count = self.get_element_text(image_build["pipeline_count"])
            return int(count[1:-2])
        except Exception as e:
            log.error("error: found pipeline_count timeout")
            log.info(e)

    def server_info(self):
        try:
            return self.get_element_text(image_build["server_scene_info"])
        except Exception as e:
            log.error("error: found server_scene_info timeout")
            log.info(e)

    def edge_info(self):
        try:
            return self.get_element_text(image_build["edge_scene_info"])
        except Exception as e:
            log.error("error: found edge_scene_info timeout")
            log.info(e)

    def cloud_info(self):
        try:
            return self.get_element_text(image_build["cloud_scene_info"])
        except Exception as e:
            log.error("error: found cloud_scene_info timeout")
            log.info(e)

    def embedded_info(self):
        try:
            return self.get_element_text(image_build["embedded_scene_info"])
        except Exception as e:
            log.error("error: found embedded_scene_info timeout")
            log.info(e)

    def type_info(self):
        try:
            return self.get_element_text(image_build["pipeline_info_type"])
        except Exception as e:
            log.error("error: found pipeline_info_type timeout")
            log.info(e)

    def name_info(self):
        try:
            return self.get_element_text(image_build["pipeline_info_name"])
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def group_info(self):
        try:
            return self.get_element_text(image_build["pipeline_info_group"])
        except Exception as e:
            log.error("error: found pipeline_info_group timeout")
            log.info(e)

    def format_info(self):
        try:
            return self.get_element_text(image_build["pipeline_info_format"])
        except Exception as e:
            log.error("error: found pipeline_info_format timeout")
            log.info(e)

    def arch_info(self):
        try:
            return self.get_element_text(image_build["pipeline_info_arch"])
        except Exception as e:
            log.error("error: found pipeline_info_arch timeout")
            log.info(e)

    def add_pipeline(self):
        """
        添加流水线弹窗
        """
        try:
            self.click(image_build["pipeline_button"])
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def div_display_check(self):
        """弹窗显示检查"""
        try:
            div = self.find_element(image_build['div'])
            if div.is_displayed():
                div_info = '添加流水线弹窗显示正常'
            else:
                div_info = '流水线弹窗显示不存在'

            return div_info
        except Exception as e:
            log.error("error: found pipeline_button timeout")
            log.info(e)

    def get_div_title(self):
        """获取弹窗标题"""
        try:
            title_txt = self.get_element_text(image_build["div_title"])
            return title_txt
        except Exception as e:
            log.error("error: found div_title timeout")
            log.info(e)

    def div_cancel(self):
        """
        流水线弹窗——取消按钮
        """
        try:
            self.click(image_build["div_cancel"])
        except Exception as e:
            log.error("error: found div_cancel_button timeout")
            log.info(e)

    def div_close(self):
        """
        流水线弹窗——关闭按钮
        """
        try:
            self.click(image_build["div_close"])
        except Exception as e:
            log.error("error: found div_close timeout")
            log.info(e)

    def div_next_page_button(self):
        """
        流水线弹窗--下一页按钮
        """
        try:
            self.click(image_build["div_next_page_button"])
        except Exception as e:
            log.error("error: found div_next_page_button timeout")
            log.info(e)

    def div_last_page_button(self):
        """
        流水线弹窗--上一页按钮
        """
        try:
            self.click(image_build["div_last_page_button"])
        except Exception as e:
            log.error("error: found div_last_page_button timeout")
            log.info(e)

    def div_confirm_button(self):
        """
        流水线弹窗--确认按钮
        """
        try:
            self.click(image_build["div_confirm"])
        except Exception as e:
            log.error("error: found div_confirm_button timeout")
            log.info(e)

    def div_title(self):
        """
        流水线弹窗——弹窗标题
        """
        try:
            title_name = self.get_element_text(image_build["div_title"], 'aria-label')
            return title_name
        except Exception as e:
            log.error("error: found div_title timeout")
            log.info(e)

    def pipeline_scene(self, scene=None):
        """
        流水线弹窗——流水线场景
        """
        if scene == "server":
            try:
                self.click(image_build["server_scene"])
            except Exception as e:
                log.error("error: found server_scene timeout")
                log.info(e)
        elif scene == "edge_computing":
            try:
                self.click(image_build["edge_computing_scene"])
            except Exception as e:
                log.error("error: found edge_computing timeout")
                log.info(e)
        elif scene == "cloud_computing":
            try:
                self.click(image_build["cloud_computing_scene"])
            except Exception as e:
                log.error("error: found cloud_computing_scene timeout")
                log.info(e)
        elif scene == "embedded":
            try:
                self.click(image_build["embedded_scene"])
            except Exception as e:
                log.error("error: found embedded_scene timeout")
                log.info(e)
        else:
            log.error("error: the scene is invalid !")

    def pipeline_type(self, pipeline_type=None):
        """
        流水线弹窗——流水线类型
        """
        if pipeline_type == "custom_image":
            try:
                self.click(image_build['custom_image'])
            except Exception as e:
                log.error("error: found custom_image timeout")
                log.info(e)
        elif pipeline_type == "version_image":
            try:
                self.click(image_build['version_image'])
            except Exception as e:
                log.error("error: found version_image timeout")
                log.info(e)
        else:
            log.error("error: the pipeline_type is invalid !")

    def pipeline_group(self):
        """
        流水线弹窗——流水线分组名称
        """
        try:
            self.group_name = self.find_element(image_build["pipeline_group"])
        except Exception as e:
            log.error("error: found pipeline_group timeout")
            log.info(e)

    def pipeline_name(self):
        """
        流水线弹窗——流水线名称
        """
        try:
            self.PipelineName = self.find_element(image_build["pipeline_name"])
        except Exception as e:
            log.error("error: found pipeline_name timeout")
            log.info(e)

    @staticmethod
    def pipeline_type_init(pipeline_type=None):
        """
        初始化——流水线类型map
        """
        if pipeline_type == "custom_image":
            image_dict = {"iso": 1, "iso_normal": 2, "docker": 1, "mini_docker": 2, "qcow2": 3, "cpio": 1,
                          "mini_cpio": 2, "raspi": 3}
        elif pipeline_type == "version_image":
            image_dict = {"iso": 1, "docker": 1, "qcow2": 2, "stratovirt": 3, "raspi": 1}
        else:
            image_dict = None

        return image_dict

    def pipeline_format(self, pipeline_type=None, pipeline_format=None):
        """
        流水线弹窗——流水线格式
        """
        image_dict = self.pipeline_type_init(pipeline_type)
        if image_dict is not None:
            try:
                if image_dict[pipeline_format] == 1:
                    self.click(image_build["image_format_iso"])
                elif image_dict[pipeline_format] == 2:
                    self.click(image_build["image_format_iso_normal"])
                else:
                    self.click(image_build["image_format_qcow2"])
            except Exception as e:
                log.error("error: found pipeline_format timeout")
                log.info(e)
        else:
            log.error("error: the pipeline_type is invalid !")

    def pipeline_arch(self, build_arch=None):
        """
        流水线弹窗——流水线架构
        """
        if build_arch is not None:
            try:
                if build_arch == "arm":
                    self.click(image_build["arch_arm"])
                else:
                    self.click(image_build["arch_x86"])
            except Exception as e:
                log.error("error: found build_arch timeout")
                log.info(e)
        else:
            log.error("error: the pipeline_type is invalid !")

    def pipeline_send_name(self, group_name=None, pipeline_name=None):
        """
        添加流水线分组名和流水线名称
        """
        self.PipelineName.send_keys(pipeline_name)
        self.group_name.send_keys(group_name)
        time.sleep(1)
        self.click(image_build["first_pipeline_group"])

    def group_name_length_info_valid(self):
        """
        获取流水线分组名长度校验提示信息
        """
        try:
            return self.get_element_text(image_build["group_valid_info"])
        except Exception as e:
            log.error("error: found group_valid_info timeout")
            log.info(e)

    def pipeline_name_length_info_valid(self):
        """
        获取流水线名称长度校验提示信息
        """
        try:
            return self.get_element_text(image_build["name_valid_info"])
        except Exception as e:
            log.error("error: found name_valid_info timeout")
            log.info(e)

    def select_first_pipeline(self):
        """
        进入第一条流水线
        """
        try:
            self.click(image_build["first_pipeline"])
        except Exception as e:
            log.error("error: found first_pipeline timeout")
            log.info(e)

    def pipeline_delete_button(self):
        """
        流水线删除按钮
        """
        try:
            self.click(image_build["delete_button"])
        except Exception as e:
            log.error("error: found delete_button timeout")
            log.info(e)

    def pipeline_alert_confirm(self):
        """
        流水线删除弹窗确认按钮
        """
        try:
            self.click(image_build["alert_confirm"])
        except Exception as e:
            log.error("error: found alert_confirm timeout")
            log.info(e)

    def alert_message(self):
        """
        获取弹窗元素
        """
        self.mouse_move(image_build["assert"])
        alert = self.find_element(image_build["assert"])

        return alert.text

    def add_pipeline_image_build(self, image_type, pipeline_group, pipeline_name, image_format, arch):
        """
        添加流水线
        """
        self.pipeline_type(image_type)
        self.pipeline_format(image_type, image_format)
        self.pipeline_arch(arch)
        time.sleep(1)
        self.pipeline_name()
        self.pipeline_group()
        self.pipeline_send_name(pipeline_group, pipeline_name)
        time.sleep(3)
        self.div_confirm_button()
        time.sleep(2)
        self.driver.implicitly_wait(30)
        info = self.alert_message()

        return info
